﻿#region Imports
using System;
using System.Drawing;
using System.Windows.Forms;
using BulkRenamer.Util;
using BulkRenamer.Util.Enums;
#endregion

namespace CheckerWin
{
    public partial class uclOptionCheckerWin : UserControl
    {
        #region Constructors
        public uclOptionCheckerWin(bool defaultValue)
        {
            InitializeComponent();
            chkOption.CheckedChanged += new EventHandler(chkOption_CheckedChanged);
            radOptionY.CheckedChanged += new EventHandler(radOption_CheckedChanged);
            radOptionN.CheckedChanged += new EventHandler(radOption_CheckedChanged);

            chkOption.Checked = defaultValue;
            SupressAutoUpdate = false;
        }
        #endregion

        #region Enums
        public enum eEventCallInit
        {
            Pre = -1,
            //ByPass = 0,
            Post = 1
        }
        #endregion

        #region Properties
        private eEventCallInit eventCallInit = eEventCallInit.Post;
        public eEventCallInit EventCallInit
        {
            get { return eventCallInit; }
            private set { eventCallInit = value; }
        }

        public string CheckerText
        {
            set { chkOption.Text = value; }
        }

        public bool SupressAutoUpdate { get; private set; }

        private bool checkerValue;
        public bool CheckerValue
        {
            get { return checkerValue; }
            set { chkOption.Checked = checkerValue = value; }
        }
        #endregion

        public void InitControls(ContentAlignment checkAlign, string optionY, string optionN)
        {
            // Alignment
            ContentAlignment align = Utilities.GetOnlyLR(checkAlign, ContentAlignment.MiddleLeft);
            radOptionY.CheckAlign = radOptionN.CheckAlign = align;
            radOptionY.TextAlign = radOptionN.TextAlign = Utilities.InvertAlignment(align, ContentAlignment.MiddleRight, Enumerators.eInversionMode.Horizontal);

            // Text
            radOptionY.Text = optionY;
            radOptionN.Text = optionN;
        }

        private void radOption_CheckedChanged(object sender, EventArgs e)
        {
            EvaluateColours();
            RadioButton rad = (RadioButton)sender;
            if (rad.Checked && !SupressAutoUpdate)
            {
                SupressAutoUpdate = true;
                chkOption.Checked = rad.Name.EndsWith("Y");
                SupressAutoUpdate = false;
            }
        }

        private void chkOption_CheckedChanged(object sender, EventArgs e)
        {
            // Pre call
            if (EventCallInit == eEventCallInit.Pre)
                if (ValueChanged != null) ValueChanged(CheckerValue, e);

            // Bypass
            //if (EventCallInit != eEventCallInit.ByPass)
            {
                EvaluateColours();
                CheckerText = chkOption.CheckState.ToString();
                if (!SupressAutoUpdate)
                {
                    SupressAutoUpdate = true;
                    if (chkOption.Checked) radOptionY.Checked = true; else radOptionN.Checked = true;
                    SupressAutoUpdate = false;
                }
            }

            // Post call
            if (EventCallInit == eEventCallInit.Post)
                if (ValueChanged != null) ValueChanged(CheckerValue, e);
        }


        private void EvaluateColours()
        {
            this.BackColor = (chkOption.Checked ? Color.Green : Color.Red);
        }

        public EventHandler ValueChanged;

        public void TogglValues()
        {
            switch (EventCallInit)
            {
                case uclOptionCheckerWin.eEventCallInit.Pre:
                    EventCallInit = uclOptionCheckerWin.eEventCallInit.Post;
                    break;
                case uclOptionCheckerWin.eEventCallInit.Post:
                    EventCallInit = uclOptionCheckerWin.eEventCallInit.Pre;
                    break;
                default:
                    break;
            }
        }
    }
}
